﻿using CTM.Web.Services;
using CTM.Web.Utils;
using System;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;

namespace CTM.Web.Controllers
{
    public class NotificationController : Controller
    {
        private INotificationService notificationService;

        public NotificationController(INotificationService notificationService)
        {
            this.notificationService = notificationService;
        }

        public ActionResult Redirect(Guid? id)
        {
            if (!id.HasValue) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var notification = notificationService.GetById(id);
            if (notification == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);

            notification.IsDeleted = true;
            notificationService.Update(notification);

            string category = string.Empty;
            string controller = "Home";
            string action = "Index";
            string postId = string.Empty;
            string empId = string.Empty;
            string url = string.Empty;

            switch (notification.Type)
            {
                case CTM.Web.Utils.NotificationType.Post:
                    category = notification.Post.Category.Name;
                    postId = notification.Post.Id.ToString();
                    empId = notification.Post.Employee.Id;
                    break;

                case CTM.Web.Utils.NotificationType.Comment:
                    category = notification.Comment.Post.Category.Name;
                    postId = notification.Comment.Post.Id.ToString();
                    empId = notification.Comment.Post.Employee.Id;
                    break;
            }

            switch (category)
            {
                case "Conversation":
                    controller = "Conversation";
                    url = Url.Action(action, controller, new { id = empId }) + "#" + postId;
                    break;

                case "Feedback":
                    controller = "Feedback";
                    url = Url.Action(action, controller, new { id = empId }) + "#feedback-" + postId;
                    break;
            }

            return Redirect(url);
        }

        public ActionResult Delete(Guid? id)
        {
            bool success = false;

            if (id.HasValue)
            {
                var notification = notificationService.GetById(id);
                if (notification != null)
                {
                    notification.IsDeleted = true;
                    notificationService.Update(notification);

                    success = true;
                }
            }

            var result = new
            {
                success = success
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        
    }
}